<?php

require('../bootstrap.php');

if ($session->is_set('author_id'))
{
	redirect('pages.php');
}

$errors = array();
if (isset($_POST['submit']))
{
	$post = escape_array($_POST);
	
	if ($post['username'] == '')
	{
		$errors[] = "Username is empty";
	}
	
	if ($post['password'] == '')
	{
		$errors[] = "Password is empty";
	}
	
	if (sizeof($errors) <= 0)
	{
		$id = authenticate($post['username'], $post['password']);
		if ($id != null)
		{
			$session->set('author_id', $id);
			if (isset($post['redirect_url']))
			{
				redirect($post['redirect_url']);
			}
			else
			{
				redirect('pages.php');
			}
		}
		$errors[] = "Wrong username/password combination.";
	}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Sider</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<link rel="stylesheet" type="text/css" href="../stylesheet/style.css" />
	
	</head>
	<body>
		<div class="AUI_Header">
			<h1>Admin</h1>
		</div>
		<div class="AUI_Content">
			<?
			if (sizeof($errors) > 0)
			{
				?>
				<div class="errors">
				Errors
				<ul>
					<?
					foreach ($errors as $error)
					{
						?>
						<li><? echo $error ?></li>
						<?
					}
					?>
				</ul>
				</div>
				<?
			}
			?>
			<form method="post">
				<table>
					<tr>
						<td>Brugernavn:</td>
						<td><input type="text" name="username" /></td>
					</tr>
					<tr>
						<td>Password:</td>
						<td><input type="password" name="password" /></td>
					</tr>
					<tr>
						<td></td>
						<td><input type="submit" name="submit" value="Log ind"></td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>